/**
 * 
 */
package pt.mleiria.network;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import pt.mleiria.network.utils.ServerConstants;

/**
 * @author NB13712
 * 
 */
public class CommandBot {
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			// Get command from user
			final BufferedReader inFromUser = new BufferedReader(
					new InputStreamReader(System.in));
			// connect to server
			final Socket clientSocket = new Socket(ServerConstants.HOST, ServerConstants.PORT);
			// Open channel for sending data
			final DataOutputStream outToServerBot = new DataOutputStream(
					clientSocket.getOutputStream());
			// Open channel for receiving data
			final BufferedReader inFromServerBot = new BufferedReader(
					new InputStreamReader(clientSocket.getInputStream()));
			// Dummy cmdId just for initializing
			int cmdId = 0x09;

			while (cmdId != 0x00) {
				cmdId = Integer.parseInt(inFromUser.readLine());
				final Command command = Command.parse(cmdId);
				switch (command) {
				case LEFT:
					outToServerBot.writeByte(Command.LEFT.getCmd());
					break;
				case RIGHT:
					outToServerBot.writeByte(Command.RIGHT.getCmd());
					break;
				case FORWARD:
					outToServerBot.writeByte(Command.FORWARD.getCmd());
					break;
				case BACK:
					outToServerBot.writeByte(Command.BACK.getCmd());
					break;
				case STOP:
					outToServerBot.writeByte(Command.STOP.getCmd());
					break;
				default:
					outToServerBot.writeByte(Command.STOP.getCmd());
					break;

				}
				System.out.println("Command sent id:" + cmdId + ";name: "
						+ Command.parse(cmdId));
			}

			int cmdRec = inFromServerBot.read();
			System.out.println("FROM SERVER: " + cmdRec);
			clientSocket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
